var app;

function quickJoin(gid) {
  const that = this;
  $.get({
    url: app.API_BASE_URL + '/api/gomoku/join?u-nick='
        + app.form.userNick + "&g-id=" + gid,
    dataType: 'json',
    success: function (resp) {
      if (resp.success) {
        window.location.href = decodeURIComponent(resp.message);
      } else {
        that.$message({
          message: "加入失败，" + resp.message,
          type: "error",
        });
      }
    }
  });
}

app = new Vue({
  el: "#app",
  data: function () {
    return {
      form: {
        userNick: '',
        gameName: '',
      },
      createHide: '0',
      showJoin: false,
      gameId: '',
      createLoading: false,
      sendTestLoading: false,

      API_BASE_URL: 'http://106.14.115.245:12345',
      // API_BASE_URL: 'http://127.0.0.1:12345',
    }
  },
  mounted: function () {
    // alert("123");
  },
  methods: {

    showJoinPage() {
      this.showJoin = true;
    },
    hideJoinPage() {
      this.showJoin = false;
    },
    resetForm() {
      Object.assign(this.form, this.$options.data().form)
    },
    isEmptyStr(str) {
      return str == null || str.trim().length === 0;
    },

    quickJoin: function (that) {
      $.get({
        url: this.API_BASE_URL + '/api/gomoku/join?u-nick='
            + this.form.userNick + "&g-id=" + gId,
        dataType: 'json',
        success: function (result) {
          if (result.success) {
            window.location.href = decodeURIComponent(result.message);
          } else {
            that.$message({
              message: "加入失败，" + result.message,
              type: "error",
            });
          }
        }
      });
    },
    joinNow: function () {
      if (isEmptyStr(this.gameId)) {
        this.$message({
          message: "请输入正确的游戏编号（六位数字字母组合）",
          type: "warning",
        });
        return;
      }
      let that = this;
      $.get({
        url: this.API_BASE_URL + '/api/gomoku/join?u-nick='
            + this.form.userNick + "&g-id=" + this.gameId,
        dataType: 'json',
        success: function (resp) {
          if (resp.success) {
            window.location.href = decodeURIComponent(resp.message);
          } else {
            that.$message({
              message: "加入失败，" + resp.message,
              type: "error",
            });
          }
        }
      });
    },
    createSubmit: function () {
      this.createLoading = true;
      let that = this;
      $.get({
        url: this.API_BASE_URL + '/api/gomoku/create?u-nick='
            + this.form.userNick + "&g-name=" + this.form.gameName + "&g-hide="
            + this.createHide,
        dataType: "json",
        success: function (res) {
          that.createLoading = false;
          if (res.success) {
            window.location.href = decodeURIComponent(res.message);
          } else {
            that.$message({
              message: "游戏创建失败，" + res.message,
              type: "error",
            });
          }
        }
      })
    },

    startManMachineBattle: function () {
      let that = this;
      $.get({
        url: this.API_BASE_URL + '/api/gomoku/create/ai?u-nick='
            + this.form.userNick + "&g-name=" + this.form.gameName,
        dataType: "json",
        success: function (res) {
          that.createLoading = false;
          if (res.success) {
            window.location.href = decodeURIComponent(res.message);
          } else {
            that.$message({
              message: "游戏创建失败，" + res.message,
              type: "error",
            });
          }
        }
      })
    }
  }

});